package com.helen.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.helen.event.InfoEnent;
import com.helen.global.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

/**
 * Created by wangyang on 2016/10/8 0008.
 */
public class ShareUtils {
    /**
     * 演示调用ShareSDK执行分享
     *
     * @param context
     * @param platformToShare 指定直接分享平台名称（一旦设置了平台名称，则九宫格将不会显示）
     * @param showContentEdit 是否显示编辑页
     */

    public static void showShare(final Context context, /*final String platformToShare, boolean showContentEdit,*/ final String shareUrl, final String title, final String content, String imgUrl) {
        final OnekeyShare oks = new OnekeyShare();
        oks.setSilent(true);
//        if (platformToShare != null) {
//            oks.setPlatform(platformToShare);
//        }
        //ShareSDK快捷分享提供两个界面第一个是九宫格 CLASSIC  第二个是SKYBLUE
        oks.setTheme(OnekeyShareTheme.CLASSIC);
        // 令编辑页面显示为Dialog模式
        oks.setDialogMode(true);
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();


        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                MyLog.e("yang", "platform.getName()==" + platform.getName());

            }
        });

        oks.setTitle(title);
        oks.setTitleUrl(shareUrl);
        oks.setText(content);
        if (TextUtils.isEmpty(imgUrl)) {
            try {
                AssetsFileUtils.getImageFromAssetsFile(context, "share_icon.png");
            } catch (Exception e) {

            }
            imgUrl = Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_icon.png";
            oks.setImagePath(imgUrl);
            MyLog.e("yang", "setImagePath=====" + imgUrl);
        } else {
            oks.setImageUrl(imgUrl);
            MyLog.e("yang", "setImageUrl===" + imgUrl);
        }

        oks.setUrl(shareUrl); //微信不绕过审核分享链接
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(final Platform platform, int i, HashMap<String, Object> hashMap) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {

                        MyLog.e("yang", "platform==" + platform + "\n" + "onComplete");
                        if (platform.isClientValid()) {
                            MyLog.e("yang", "1111111111-----------" + platform.getName());
                            EventBus.getDefault().post(new InfoEnent(1, getType(platform)));

                        }

                    }
                });

            }

            @Override
            public void onError(final Platform platform, int i, Throwable throwable) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        MyLog.e("yang", "platform==" + platform + "\n" + "onError");
                        if (platform.isClientValid()) {
                            MyLog.e("yang", "22222222------------");
                            EventBus.getDefault().post(new InfoEnent(2, getType(platform)));
                        }

                    }
                });

            }

            @Override
            public void onCancel(final Platform platform, int i) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        MyLog.e("yang", "platform==" + platform + "\n" + "onCancel");
                        if (platform.isClientValid()) {
                            EventBus.getDefault().post(new InfoEnent(3, getType(platform)));
                        }

                    }
                });

            }
        });
        // 启动分享
        oks.show(context);
    }

    public static String getType(Platform platform) {
        if ("QQ".equals(platform.getName())) {
            return "qq";
        }
        if ("QZone".equals(platform.getName())) {
            return "qqZone";
        }
        if ("Wechat".equals(platform.getName())) {
            return "weChat";
        }
        if ("WechatMoments".equals(platform.getName())) {
            return "weChatCircle";
        }
        return "";
    }


    public static void showShare(Activity activity,final String imgUrl) {
        final OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
//        try {
//            AssetsFileUtils.getImageFromAssetsFile(activity, "share_scan_img.png");
//        } catch (Exception e) {
//
//        }
//        oks.setImagePath(Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_scan_img.png");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setImageUrl(imgUrl);
//        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
//            @Override
//            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
//                if (platform.getName().equalsIgnoreCase(QQ.NAME)) {
//                    paramsToShare.setText(null);
//                    paramsToShare.setTitle(null);
//                    paramsToShare.setTitleUrl(null);
////                    paramsToShare.setImagePath(Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_scan_img.png");
//                    oks.setUrl("https://www.baidu.com/");
//                    // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//                    oks.setComment("我是测试评论文本");
//                    // site是分享此内容的网站名称，仅在QQ空间使用
//                    oks.setSite("善回收");
//                    // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//                    oks.setSiteUrl("https://www.baidu.com/");
//                    oks.setImageUrl(imgUrl);
//                } else if (platform.getName().equalsIgnoreCase(QZone.NAME)) {
//                    paramsToShare.setText(null);
//                    paramsToShare.setTitle(null);
//                    paramsToShare.setTitleUrl(null);
//                    oks.setImageUrl(imgUrl);
////                    paramsToShare.setImagePath(Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_scan_img.png");
//                } else if (platform.getName().equalsIgnoreCase(Wechat.NAME)) {
//                    paramsToShare.setText(null);
//                    paramsToShare.setTitle(null);
//                    paramsToShare.setTitleUrl(null);
//                    oks.setImageUrl(imgUrl);
////                    paramsToShare.setImagePath(Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_scan_img.png");
//                } else if (platform.getName().equalsIgnoreCase(WechatMoments.NAME)) {
//                    paramsToShare.setText(null);
//                    paramsToShare.setTitle(null);
//                    paramsToShare.setTitleUrl(null);
//                    oks.setImageUrl(imgUrl);
////                    paramsToShare.setImagePath(Environment.getExternalStorageDirectory() + Constants.GLIDE_CARCH_DIR + "/share_scan_img.png");
//                }
//            }
//        });
        oks.show(activity);
    }

}
